﻿using UnityEngine;
using UnityEngine.SceneManagement;
using UnityEngine.UI;
using System.Collections;

public class GameManager : Singleton<GameManager>
{
    public PlaneController PlayerPlane;
    public Image BlackMaskUI;
    public GameObject LoseUIObject;
    public GameObject WinUIObject;
    public GameObject PauseUIObject;
    public int CurrentSceneIndex;
    public int NextSceneIndex;
    [Range(0, 2), Tooltip("0表示第一封信，以此类推")]
    public int EnableLetter;
    /// <summary>
    /// 纸飞机是否被抓，被撞，撞墙
    /// </summary>
    [HideInInspector]
    public byte GameState = 0;
    protected override void Awake()
    {
        base.Awake();
    }
    private void Update()
    {
        if (GameState == Constants.GameState_Common && Input.GetButtonDown(Constants.Text_Input_GamePause))
        {
            Time.timeScale = 0f;
            GameState = Constants.GameState_Pause;
            PauseUIObject.SetActive(true);
        }
        else if(GameState == Constants.GameState_Common && Input.GetButtonDown(Constants.Text_Input_GamePause))
        {
            Time.timeScale = 1f;
            PauseUIObject.SetActive(false);
            GameState = Constants.GameState_Common;
        }
    }
    IEnumerator Lose()
    {
        float t = 0f;
        Color c = Color.black;
        c.a = 0f;
        float blackMaskTime = Constants.ShowBlackMaskTime / 2f;
        while (t < blackMaskTime)
        {
            t += Time.deltaTime;
            c.a = Mathf.Lerp(0f, 0.5f, t / blackMaskTime);
            BlackMaskUI.color = c;
            yield return new WaitForEndOfFrame();
        }
        c.a = 0.5f;
        BlackMaskUI.color = c;
        LoseUIObject.SetActive(true);
    }
    IEnumerator ShowBlackMaskAndLoadScene()
    {
        float t = 0f;
        Color c = Color.black;
        c.a = 0f;
        while (t < Constants.ShowBlackMaskTime)
        {
            t += Time.deltaTime;
            c.a = Mathf.Lerp(0f, 1f, t / Constants.ShowBlackMaskTime);
            BlackMaskUI.color = c;
            yield return new WaitForEndOfFrame();
        }
        c.a = 1f;
        BlackMaskUI.color = c;
        SceneManager.LoadScene(NextSceneIndex);
    }
    public void LoadNextScene()
    {
        StartCoroutine(ShowBlackMaskAndLoadScene());
    }
    public void ShowWinMessageBox()
    {
        PlayerPlane.enabled = false;
        WinUIObject.SetActive(true);
        GlobalData.LetterCanbeReads[EnableLetter] = true;
    }
    public void ShowLoseMessageBox()
    {
        StartCoroutine(Lose());
    }
    public void Restart()
    {
        SceneManager.LoadScene(CurrentSceneIndex);
    }
    public void ReturnMenu()
    {
        GlobalData.SaveNextScene = GameState == Constants.GameState_Win ? NextSceneIndex : CurrentSceneIndex;
        //如果不重新设置timeScale的话会影响接下来读取的场景的Update和协程
        Time.timeScale = 1f;
        GlobalData.Save();
        SceneManager.LoadScene(0);
    }
    public void ContinueGame()
    {
        Time.timeScale = 1f;
        GameState = Constants.GameState_Common;
        PauseUIObject.SetActive(false);
    }
}
